package net;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketAddress;
import java.net.SocketException;

public class DatagramPacketDemo {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Thread ts = new Thread(new DatagramPacketSend());
		Thread tr = new Thread(new DatagramPacketReceive());

		ts.start();
		tr.start();

	}

	public static void printStatus(DatagramSocket ds) throws SocketException {
		System.out.println("\nisBound  =" + ds.isBound());
		System.out.println("getBroadcast =" + ds.getBroadcast());
		System.out.println("isClosed  =" + ds.isClosed());
		System.out.println("isConnected  =" + ds.isConnected());
		System.out.println("getInetAddress  =" + ds.getInetAddress());
		System.out.println("getLocalAddress  =" + ds.getLocalAddress());
		System.out.println("getLocalPort  =" + ds.getLocalPort());
		System.out.println("getPort  =" + ds.getPort());
		System.out.println("getLocalSocketAddress  ="
				+ ds.getLocalSocketAddress());
		System.out.println("getRemoteSocketAddress  ="
				+ ds.getRemoteSocketAddress());
	}

}

class DatagramPacketSend implements Runnable {

	public void run() {
		System.out.println("send data :");
		try {
			DatagramSocket ds = new DatagramSocket();

			// 确定发送方的IP地址及端口号，地址为本地机器地址
			int port = 8888;
			InetAddress ip = InetAddress.getLocalHost();
			byte[] b = "中国共产党第十八次代表大会".getBytes();
			DatagramPacket dp = new DatagramPacket(b, b.length, ip, port);

			ds.send(dp);

			// 确定接受反馈数据的缓冲存储器，即存储数据的字节数组
			byte[] getBuf = new byte[1024];

			// 创建接受类型的数据报
			DatagramPacket getPacket = new DatagramPacket(getBuf, getBuf.length);

			// 通过套接字接受数据
			ds.receive(getPacket);

			// 解析反馈的消息，并打印
			String backMes = new String(getBuf, 0, getPacket.getLength());
			System.out.println("接受方返回的消息：" + backMes);

			// 关闭套接字
			ds.close();
		} catch (SocketException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

class DatagramPacketReceive implements Runnable {

	public void run() {
		System.out.println("receive data :");
		byte[] b = new byte[1024];
		DatagramPacket dp = new DatagramPacket(b, b.length);
		try {
			DatagramSocket ds = new DatagramSocket(8888);
			// DatagramPacketDemo.printStatus(ds);
			ds.receive(dp);
			System.out.println(dp.getData());

			// 通过数据报得到发送方的IP\端口号\套接字地址，并打印
			InetAddress sendIP = dp.getAddress();
			int sendPort = dp.getPort();
			SocketAddress sendAddress = dp.getSocketAddress();
			// 确定要反馈发送方的消息内容，并转换为字节数组
			String feedback = "接收方说：我收到了！";
			byte[] backBuf = feedback.getBytes();
			// 创建发送类型的数据报
			DatagramPacket sendPacket = new DatagramPacket(backBuf,
					backBuf.length, sendAddress);
			// 通过套接字发送数据
			ds.send(sendPacket);
			// 关闭套接字
			ds.close();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}